package library.web.servlet.ajax;

import library.domain.User;
import library.persistence.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

public class IsPasswordValidate extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String inputCode = req.getParameter("inputCode");
        String code = (String) session.getAttribute("code");
        int selectManager = Integer.parseInt(req.getParameter("selectManager"));

        AccountService accountService = new AccountService();
        User user = accountService.getAccount(username);

        String correct_password = user.getPassword();
        int correct_Manager = user.getIsManager();

        if(inputCode.equals(code)){//验证码输入正确
            if(correct_password.equals(password)){//密码合法
                if(correct_Manager == selectManager){
                    if(correct_Manager == 1){
                        out.print("Manager Validate");//合法且为管理员身份
                    }else {
                        out.print("Validate");//密码和身份都合法
                    }
                    session.setAttribute("loginAccount",user);
                }else{
                    if(correct_Manager == 1){
                        out.print("Manager Validate");//合法且为管理员身份
                    }else{
                        out.print("Status Not Validate");//身份不合法
                    }
                }
            }else {
                out.print("Password Not Validate");//密码不合法
            }

        }else{
            out.print("Verify Not Validate");
        }
    }

}
